export default {
    searchHandle(data) {
        this.searchData = {...data}
        this.getUserList();
    },
    /**
     * 全选或全不选时调用方法
     * @param rows
     */
    toggleSelection(rows) {
        if (rows) {
            rows.forEach(row => {
                this.$refs.multipleTable.toggleRowSelection(row);
            });
        } else {
            this.$refs.multipleTable.clearSelection();
        }
    },
    /**
     * 选项改变时调用方法
     * @param rows
     */
    handleSelectionChange(rows) {
        this.multipleSelection = rows;
        this.delBtnStu = rows.length == 0
        console.log(rows)
    },
    pagerChange(option) {
        this.getData("user_pageroption");
        console.log(option)
        this.pageOption = option;
        this.setData("user_pageroption", "hasd")
        this.getUserList();
    },
    /**
     * 获取用户列表
     */
    getUserList() {
        this.tableLoading = true;
        this.$axios.post('/sys/user/list', {
                searchData: this.searchData,
                pageOption: this.pageOption,
                current: this.pageOption.current,
                size: this.pageOption.size
        }).then(res => {

            this.tableData = res.data.data.records
            console.log(res.data.data)
            this.pageOption.current = res.data.data.current
            this.pageOption.size = res.data.data.size
            this.pageOption.total = res.data.data.total
            this.tableLoading = false;
        })
    },
    /**
     * 显示新增对话框
     */
    showAdd() {
        this.formTitle = "新增用户"
        this.editForm = { status: 1 }
        this.dialogFormVisible = true
    },
    /**
     * 显示编辑对话框
     * @param id
     */
    showEdit(row) {
        this.$axios.get("/sys/user/info/" + row.id).then(res => {
            this.formTitle = "编辑用户"
            this.editForm = res.data.data
            this.dialogFormVisible = true
        })
    },
    /**
     * 提交新增/编辑用户
     * @param data
     */
    submitHandle(data) {
        this.$axios.post('/sys/user/' + (data.id ? "update" : "save"), data)
            .then(res => {
                if (res.data.code === "00000") {
                    this.dialogFormVisible = false;
                    this.getUserList();
                    this.msgBox();
                } else {
                    this.msgBox(false);
                }
            })
    },
    /**
     * 打开分配角色窗对话框
     * @param id
     */
    roleHandle(id) {
        this.roleDialogFormVisible = true
        this.roleTreeloading = true
        this.$axios.get("/sys/user/info/" + id).then(res => {
            const user = res.data.data
            var roleIds = []
            user.roles.forEach(row => {
                roleIds.push(row.id)
            })
            this.$refs.roleTree.setCheckedKeys(roleIds);
            this.roleForm = res.data.data
            this.roleTreeloading = false
        })

    },
    /**
     * 分配角色
     * @param formName
     */
    submitRoleForm(formName) {
        var roleIds = []
        roleIds = this.$refs.roleTree.getCheckedKeys()
        this.$axios.post("/sys/user/role/" + this.roleForm.id, roleIds).then(res => {
            // this.resetForm(formName)
            this.getUserList()
            localStorage.removeItem("menuList")
            localStorage.removeItem("permList")

            this.msgBox();
            this.roleDialogFormVisible = false
        })
    },
    /**
     * 重置密码
     * @param id
     * @param username
     */
    repassHandle(id, username) {
        this.$confirm('将重置用户【' + username + '】的密码, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            this.$axios.post("/sys/user/repass", id).then(res => {
                if (res.data.code === "00000") {
                    this.msgBox();
                }
            })
        })
    },
    /**
     * 获取角色列表
     */
    getRoleList() {
        this.$axios.get("/sys/role/list").then(res => {
            this.roleTreeData = res.data.data
        })
    },
    /**
     * 删除
     * @param id
     */
    delHandle(id) {
        var ids = []
        id ? ids.push(id) : this.multipleSelection.forEach(row => {
            ids.push(row.id)
        })
        this.$axios.post("/sys/user/delete", ids).then(res => {
            this.getUserList()
            this.msgBox();
        })
    }
}